package com.own.business.question.open;

import com.own.business.question.business.service.QuestionService;
import com.own.business.question.common.em.QuestionType;
import com.own.business.question.entity.bo.QuestionBo;
import com.own.business.question.entity.map.QuestionMapVo;
import com.own.business.question.entity.open.BaseOpenQuestion;
import com.own.business.question.entity.po.Question;
import com.own.business.question.entity.query.QuestionQuery;
import com.own.business.question.entity.vo.QuestionVo;
import com.own.component.common.base.open.api.BaseOpenMethod;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * QuestionOpenMethod
 *
 * @author chenxueli
 * @date 2021/5/14 11:33
 */
@Component
public class QuestionOpenMethod extends BaseOpenMethod<
        Question,
        QuestionBo,
        QuestionVo,
        QuestionMapVo,
        QuestionQuery,
        QuestionService
        > implements BaseOpenQuestion {

    @Resource
    private QuestionService questionService;

    /**
     * 添加信息
     *
     * @param questionBoList 添加对象列表
     * @return 添加标识
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultModel<List<QuestionVo>> addBatch(List<QuestionBo> questionBoList) {
        return R.success(questionService.addBatch(questionBoList));
    }

    /**
     * 根据id获取题目信息（不需要答案，并打乱顺序）
     *
     * @param questionId 题目id
     * @return 题目信息
     */
    @Override
    public ResultModel<QuestionVo> getByIdForAnswer(Long questionId) {
        return R.success(questionService.getByIdForAnswer(questionId));
    }

    /**
     * 根据id列表获取题目列表
     *
     * @param questionIdList 题目id列表
     * @param answer         是否需要答案
     * @param shuffle        是否需要打乱
     * @return 题目列表
     */
    @Override
    public ResultModel<List<QuestionVo>> listByIds(List<Long> questionIdList, boolean answer, boolean shuffle) {
        return R.success(questionService.listByIds(questionIdList, answer, shuffle));
    }

    /**
     * 根据id列表获取题目列表
     *
     * @param questionIdList 题目id列表
     * @param answer         是否需要答案
     * @param shuffle        是否需要打乱
     * @param type           题目类型
     * @param number         题目数量
     * @return 题目列表
     */
    @Override
    public ResultModel<List<QuestionVo>> listByIds(List<Long> questionIdList, boolean answer, boolean shuffle, QuestionType type, int number) {
        return R.success(questionService.listByIds(questionIdList, answer, shuffle, type, number));
    }

    /**
     * 修改题目的可用状态
     *
     * @param pk 题目id
     * @return 修改标识
     */
    @Override
    public ResultModel<Boolean> updateDisabled(Long pk) {
        return R.success(questionService.updateDisabled(pk));
    }

    /**
     * 题目答案比对
     *
     * @param pk     题目id
     * @param answer 答案信息
     * @return 比对结果（0-1）
     */
    @Override
    public ResultModel<BigDecimal> matchAnswer(Long pk, String answer) {
        return R.success(questionService.matchAnswer(pk, answer));
    }

    /**
     * 添加总数
     *
     * @param pk 题目id
     * @return 是否成功
     */
    @Override
    public ResultModel<Boolean> addTotal(Long pk) {
        questionService.addTotal(pk);
        return R.success();
    }

    /**
     * 添加总数
     *
     * @param pkList 题目id列表
     * @return 是否成功
     */
    @Override
    public ResultModel<Boolean> addTotal(List<Long> pkList) {
        questionService.addTotal(pkList);
        return R.success();
    }

    /**
     * 根据流水id获取题目信息
     *
     * @param historyId 流水id
     * @return 题目信息
     */
    @Override
    public ResultModel<QuestionVo> getByHistoryId(Long historyId) {
        return R.success(questionService.getByHistoryId(historyId));
    }

}
